//
//  feedbackViewController.m
//  Food
//
//  Created by 15 on 15/7/25.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "feedbackViewController.h"
#import "BaseHeader.h"
@interface feedbackViewController ()<UITextViewDelegate>

@property (nonatomic, retain) UILabel *lblPlaceholder;

@end

@implementation feedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.titleView = [[[UIView alloc] initWithFrame:CGRectMake(100, 30, 100, 20)] autorelease];
    self.navigationItem.titleView.tintColor = REDCOLOR(1);
    UILabel *lblFeedback = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)] autorelease];
    lblFeedback.text = @"意见反馈";
    lblFeedback.textAlignment = NSTextAlignmentCenter;
    lblFeedback.textColor = REDCOLOR(1);
    [self.navigationItem.titleView addSubview:lblFeedback];
    self.view.backgroundColor = WHITECOLOR;
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, SCREEN_WIDTH - 20, 300)];
    self.textView.textColor = [UIColor blackColor];
    self.textView.keyboardType = UIKeyboardTypeDefault;
    self.textView.returnKeyType = UIReturnKeyDefault;
    self.textView.scrollEnabled = YES;
    self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.textView.font = [UIFont systemFontOfSize:17];
    self.textView.hidden = NO;
    self.textView.delegate = self;
    [self.textView release];
    [self.view addSubview:self.textView];
    self.textView.layer.cornerRadius = 8;

    // placeholder
    self.lblPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(3, 8, self.textView.bounds.size.width - 20, 20)];
    self.lblPlaceholder.text = @"请简单描述你的建议,我们会酌情采纳的哦";
    self.lblPlaceholder.numberOfLines = 0;
    [self.lblPlaceholder sizeToFit];
    self.lblPlaceholder.enabled = NO;
    self.lblPlaceholder.backgroundColor = [UIColor clearColor];
    [self.textView addSubview:self.lblPlaceholder];
    [self.lblPlaceholder release];

    // 提交按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction)];

}

-(void)buttonAction{


    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = @"asdfsadfsdfsadf@126.com";
    testMsg.toEmail = @"2471636907@qq.com";
    testMsg.relayHost = @"smtp.126.com";
    testMsg.requiresAuth = YES;
    if (testMsg.requiresAuth) {
        testMsg.login = @"asdfsadfsdfsadf@126.com";
        testMsg.pass = @"123456asd";
    }


    testMsg.wantsSecure = YES;
    testMsg.subject = @"反馈意见";
    testMsg.delegate = self;
    NSString *str = self.textView.text;
    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
                               str,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [testMsg send];
    });

}

- (void)messageSent:(SKPSMTPMessage *)message
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"感谢您的反馈" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    [message release];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        self.lblPlaceholder.hidden = NO;
        self.lblPlaceholder.text = @"请简单描述你的建议,我们会酌情采纳的哦";
    } else {
        self.lblPlaceholder.hidden = YES;
    }
}

- (void)resignKeyboard {
    [self.textView resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
